home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / jaq / dist / jstat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-11  |  8.0 KB  |  325 lines

  1. /* 
  2.  * jstat.c --
  3.  *
  4.  *    Perform a status request on Jaquith archive system.
  5.  *
  6.  * Copyright 1991 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/lib/forms/RCS/jstat.c,v 1.0 91/01/07 18:02:37 mottsmth Exp $ SPRITE (Berkeley)";
  18. #endif /* not lint */
  19.  
  20. #include "jaquith.h"
  21. #include "option.h"
  22. #include "jstatInt.h"
  23.  
  24. int syserr;
  25.  
  26. static char printBuf[T_MAXSTRINGLEN];
  27. static FILE *memDbg = NULL;
  28.  
  29. static int headers = 0;
  30.  
  31. Parms parms = {
  32.     "",
  33.     -1,
  34.     "",
  35.     DEF_MAIL,
  36.     DEF_DEVICES,
  37.     DEF_QUEUES,
  38.     DEF_ARCHLIST
  39. };
  40.  
  41. Option optionArray[] = {
  42.     {OPT_STRING, "server", (char *)&parms.server, "Server hostname"},
  43.     {OPT_INT, "port", (char *)&parms.port, "Port of server"},
  44.     {OPT_STRING, "arch", (char *)&parms.arch, "Name of archive for -devices -queues option"},
  45.     {OPT_STRING, "mail", (char *)&parms.mail, "Mail address"},
  46.     {OPT_TRUE, "devices", (char *)&parms.devices, "Report device info"},
  47.     {OPT_TRUE, "queues", (char *)&parms.queues, "Report queue info"},
  48.     {OPT_TRUE, "archlist", (char *)&parms.archList, "Report logical archives"}
  49. };
  50. int numOptions = sizeof(optionArray) / sizeof(Option);
  51.  
  52. static void CheckOptions       _ARGS_ ((Parms *parmsPtr));
  53. static void ProcessDevices     _ARGS_ ((int sock, int displayFlag));
  54. static void ProcessQueues      _ARGS_ ((int sock, int displayFlag));
  55. static void ProcessArchList    _ARGS_ ((int sock, int displayFlag));
  56.  
  57.  
  58. /*
  59.  *----------------------------------------------------------------------
  60.  *
  61.  * main --
  62.  *
  63.  *    Main driver for jstat program.
  64.  *
  65.  * Results:
  66.  *    none.
  67.  *
  68.  * Side effects:
  69.  *    Sends status requests across socket to Jaquith server.
  70.  *
  71.  *----------------------------------------------------------------------
  72.  */
  73.  
  74. int
  75. main(argc, argv)
  76. int argc;
  77. char *argv[];
  78. {
  79.     int sock;
  80.  
  81. /*    memDbg = fopen("jstat.mem", "w"); */
  82.     MEM_CONTROL(8192, memDbg, TRACEMEM+TRACECALLS, 4096);
  83.  
  84.     argc = Opt_Parse(argc, argv, optionArray, numOptions, 0);
  85.  
  86.     CheckOptions(&parms);
  87.  
  88.     sock = Sock_SetupSocket(parms.port, parms.server, 1);
  89.  
  90.     Sock_SendReqHdr(sock, T_CMDSTAT, 0, parms.mail,  parms.arch, 0);
  91.  
  92.     ProcessDevices(sock, parms.devices);
  93.     ProcessQueues(sock, parms.queues);
  94.     ProcessArchList(sock, parms.archList);
  95.  
  96.     close(sock);
  97.  
  98.     MEM_REPORT("jstat", ALLROUTINES, SORTBYREQ);
  99.  
  100.     return 0;
  101. }
  102.  
  103.  
  104.  
  105. /*
  106.  *----------------------------------------------------------------------
  107.  *
  108.  * ProcessDevices --
  109.  *
  110.  *    Read and possibly report device status
  111.  *
  112.  * Results:
  113.  *    none.
  114.  *
  115.  * Side effects:
  116.  *    Reads from socket. Will print to stdout if display != 0
  117.  *
  118.  *----------------------------------------------------------------------
  119.  */
  120.  
  121. static void
  122. ProcessDevices(sock, display)
  123.     int sock;                 /* server socket connection */
  124.     int display;              /* display flag. 1==print it */
  125. {
  126.     int count;
  127.     char devName[T_MAXSTRINGLEN];
  128.     char *devPtr = devName;
  129.     char hostName[T_MAXSTRINGLEN];
  130.     char *hostPtr = hostName;
  131.     char userName[T_MAXSTRINGLEN];
  132.     char *userPtr = userName;
  133.     int volId;
  134.     char volIdStr[T_MAXSTRINGLEN];
  135.     int i;
  136.  
  137.     if (Sock_ReadInteger(sock, &count) != T_SUCCESS) {
  138.     Utils_Bailout("Server died during transmission\n", BAIL_PRINT);
  139.     }
  140.  
  141.     if (display && headers) {
  142.     if (count == 0) {
  143.         printf("\nStrange. Server is not managing any devices!\n");
  144.     } else if (count == -1) {
  145.         printf("\nCouldn't get device status. (Jmgr dead?)\n");
  146.     } else {
  147.         printf("\nDevice Status:\n");
  148.     }
  149.  
  150.     }
  151.  
  152.     for (i=0; i<count; i++) {
  153.     if ((Sock_ReadString(sock, &devPtr, 0) != T_SUCCESS) ||
  154.         (Sock_ReadInteger(sock, &volId) != T_SUCCESS) ||
  155.         (Sock_ReadString(sock, &hostPtr, 0) != T_SUCCESS) ||
  156.         (Sock_ReadString(sock, &userPtr, 0) != T_SUCCESS)) {
  157.         Utils_Bailout("Server died during transmission\n", BAIL_PRINT);
  158.     }
  159.     if (display) {
  160.         if (volId == -1) {
  161.         strcpy(volIdStr, "No volume mounted.");
  162.         } else if (*userName == '\0')  {
  163.         sprintf(volIdStr, "volume %d", volId);
  164.         } else {
  165.         sprintf(volIdStr, "volume %d\t%s@%s", volId, userName, hostName);
  166.         }
  167.         printf("%s\t%s\n", devName, volIdStr);
  168.     }
  169.     }
  170. }
  171.  
  172.  
  173.  
  174. /*
  175.  *----------------------------------------------------------------------
  176.  *
  177.  * ProcessQueues --
  178.  *
  179.  *    Read and possibly report queue status
  180.  *
  181.  * Results:
  182.  *    none.
  183.  *
  184.  * Side effects:
  185.  *    Reads from socket. Will print to stdout if display != 0
  186.  *
  187.  *----------------------------------------------------------------------
  188.  */
  189.  
  190. static void
  191. ProcessQueues(sock, display)
  192.     int sock;                 /* server socket connection */
  193.     int display;              /* display flag. 1==print it */
  194. {
  195.     int count;
  196.     char hostName[80];
  197.     char *hostPtr = hostName;
  198.     char userName[80];
  199.     char *userPtr = userName;
  200.     int volId;
  201.     int i;
  202.  
  203.     if (Sock_ReadInteger(sock, &count) != T_SUCCESS) {
  204.     Utils_Bailout("Server died during transmission\n", BAIL_PRINT);
  205.     }
  206.  
  207.     if (display && headers) {
  208.     if (count == 0) {
  209.         printf("\nNo entries in device queue.\n");
  210.     } else if (count == -1) {
  211.         printf("\nCouldn't get queue status. (Jmgr dead?)\n");
  212.     } else {
  213.         printf("\nDevice Queue:\n");
  214.     }
  215.     }
  216.  
  217.     for (i=0; i<count; i++) {
  218.     if ((Sock_ReadInteger(sock, &volId) != T_SUCCESS) ||
  219.         (Sock_ReadString(sock, &hostPtr, 0) != T_SUCCESS)||
  220.         (Sock_ReadString(sock, &userPtr, 0) != T_SUCCESS)) {
  221.         Utils_Bailout("Server died during transmission\n", BAIL_PRINT);
  222.     }
  223.     if (display) {
  224.         printf("%s@%s\tvol %d\n", userName, hostName, volId);
  225.     }
  226.     }
  227. }
  228.  
  229.  
  230. /*
  231.  *----------------------------------------------------------------------
  232.  *
  233.  * ProcessArchList --
  234.  *
  235.  *    Read and possibly report archive list.
  236.  *
  237.  * Results:
  238.  *    none.
  239.  *
  240.  * Side effects:
  241.  *    Reads from socket. Will print to stdout if display != 0
  242.  *
  243.  *----------------------------------------------------------------------
  244.  */
  245.  
  246. static void
  247. ProcessArchList(sock, display)
  248.     int sock;                 /* server socket connection */
  249.     int display;              /* display flag. 1==print it */
  250. {
  251.     char archName[80];
  252.     char *archPtr = archName;
  253.  
  254.     if (display) {
  255.     if (headers) {
  256.         printf("\nLogical archives:\n");
  257.     }
  258.     while (Sock_ReadString(sock, &archPtr, 0) == T_SUCCESS) {
  259.         if (*archName) {    
  260.         printf("%s\n", archName);
  261.         } else {
  262.         break;
  263.         }
  264.     }
  265.     }
  266. }    
  267.  
  268.  
  269. /*
  270.  *----------------------------------------------------------------------
  271.  *
  272.  * CheckOptions -- 
  273.  *
  274.  *    Make sure command line options look reasonable
  275.  *
  276.  * Results:
  277.  *    none.
  278.  *
  279.  * Side effects:
  280.  *    none.
  281.  *
  282.  *----------------------------------------------------------------------
  283.  */
  284.  
  285. static void
  286. CheckOptions(parmsPtr)
  287.     Parms *parmsPtr;          /* parameter list */
  288. {
  289.     char *envPtr;
  290.  
  291.     if ((!*parmsPtr->server) &&
  292.     ((parmsPtr->server=getenv(ENV_SERVER_VAR)) == (char *)NULL)) {
  293.     parmsPtr->server = DEF_SERVER;
  294.     }
  295.     if (Utils_CheckName(parmsPtr->server, 1) == T_FAILURE) {
  296.     sprintf(printBuf,
  297.         "Bad server name: '%s'.\nUse -server or set %s environment variable.\n",
  298.         parmsPtr->server, ENV_SERVER_VAR);
  299.     Utils_Bailout(printBuf, BAIL_PRINT);
  300.     }
  301.     if (parmsPtr->port == -1) {
  302.     if ((envPtr=getenv(ENV_PORT_VAR)) == (char *)NULL) {
  303.         parmsPtr->port = DEF_PORT;
  304.     } else {
  305.         Utils_CvtInteger(envPtr, 1, SHRT_MAX, &parmsPtr->port);
  306.     }
  307.     }
  308.     if ((parmsPtr->port < 1) || (parmsPtr->port > SHRT_MAX)) {
  309.     sprintf(printBuf,
  310.         "Bad port number %d.\nUse -port or set %s environment variable\n",
  311.         parmsPtr->port, ENV_PORT_VAR);
  312.     Utils_Bailout(printBuf, BAIL_PRINT);
  313.     }
  314.     if ((!parmsPtr->mail) && 
  315.     (Utils_CheckName(parmsPtr->mail, 1) == T_FAILURE)) {
  316.     sprintf(printBuf, "Bad mail address: '%s'\n", parmsPtr->mail);
  317.     Utils_Bailout(printBuf, BAIL_PRINT);
  318.     }
  319.     if ((!parmsPtr->devices) && (!parmsPtr->queues) && (!parmsPtr->archList)) {
  320.     Utils_Bailout("Need -devices, -queues, or -archlist option\n", 
  321.         BAIL_PRINT);
  322.     }
  323. }
  324.  
  325.